package com.example.controller;

import com.example.entity.Scene;
import com.example.service.SceneService;
import com.example.util.AjaxResult;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api")
public class SceneController {

    @Autowired
    private SceneService sceneService;

    /**
     * 创建场景
     * @param scene 场景信息(name, description)及包含的设备动作列表
     * @return 创建的场景信息
     */
    @PostMapping("/scenes")
    public AjaxResult createScene(@RequestBody Scene scene) {
        Integer count = sceneService.createScene(scene);
        if (count > 0){
            return AjaxResult.success("创建成功", scene);
        }else{
            return AjaxResult.error("创建失败");
        }
    }

    /**
     * 获取场景列表
     * @param creatorId 创建者ID（可选）
     * @return 场景列表
     */
    @GetMapping("/scenes")
    public AjaxResult getScenes(@RequestParam(required = false) Long creatorId) {
        List<Scene> scenes = sceneService.getScenes(creatorId);
       if (CollectionUtils.isNotEmpty(scenes)){
           return AjaxResult.success("查询成功", scenes);
       }else{
           return AjaxResult.error("查询失败");
       }
    }

    /**
     * 执行场景
     * @param sceneId 场景ID
     * @return 执行结果
     */
    @PostMapping("/scenes/{sceneId}/execute")
    public AjaxResult executeScene(@PathVariable Long sceneId) {
        boolean result = sceneService.executeScene(sceneId);
        if (result) {
            return AjaxResult.success("执行成功");
        } else {
            return AjaxResult.error("执行失败");
        }
    }

    /**
     * 删除场景
     * @param sceneId 场景ID
     * @return 删除结果
     */
    @DeleteMapping("/scenes/{sceneId}")
    public AjaxResult deleteScene(@PathVariable Long sceneId) {
        boolean result = sceneService.deleteScene(sceneId);
        if (result) {
            return AjaxResult.success("删除成功");
        } else {
            return AjaxResult.error("删除失败");
        }
    }

}
